package fragment;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.transition.TransitionInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

import bean.BranchBean;
import fragment.detialfragment.BranchDetialFragment;
import qf.wlh.com.oneprisdent.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.RetrofitBranchFragmentInterface;

/**
 * A simple {@link Fragment} subclass.
 */
public class BranchFragment extends Fragment implements View.OnClickListener {

    private View view;
    private TextView tvTitle;
    private TextView tvContent;
    private ImageView ivImageView;
    private Retrofit retrofit;
    private FragmentManager fragmentManager;
    private List<BranchBean.DataBean> dataBeen;
    private String transitionName;
    private String tvTitleTransition;

    public BranchFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        fragmentManager = getActivity().getSupportFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_branch,container,false);
        initView();
        //网络请求数据
        retrofit = new Retrofit
                .Builder()
                .baseUrl("http://api.yixi.tv/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitBranchFragmentInterface retrofitBranchFragmentInterface = retrofit.create(RetrofitBranchFragmentInterface.class);
        retrofitBranchFragmentInterface.getData().enqueue(new Callback<BranchBean>() {
            @Override
            public void onResponse(Call<BranchBean> call, Response<BranchBean> response) {
                List<BranchBean.DataBean> data = response.body().getData();
                if (data.get(0).getTitle() != null){
                    tvTitle.setText(data.get(0).getTitle());
                }
                if (data.get(0).getDesc() != null){
                    tvContent.setText(data.get(0).getDesc());
                }
                if (data.get(0).getCover() != null){
                    Picasso.with(getContext()).load(data.get(0).getCover()).skipMemoryCache().into(ivImageView);
                }
                dataBeen = data;
            }

            @Override
            public void onFailure(Call<BranchBean> call, Throwable t) {

            }
        });
        return view;
    }

    private void initView() {
        tvTitle = ((TextView) view.findViewById(R.id.branch_fragment_title));
        tvContent = ((TextView) view.findViewById(R.id.branch_fragment_content));
        ivImageView = ((ImageView) view.findViewById(R.id.branch_fragment_imageview));

        tvTitle.setOnClickListener(this);
        tvContent.setOnClickListener(this);
        ivImageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {Bundle bundle = new Bundle();
        BranchDetialFragment branchDetialFragment = new BranchDetialFragment();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//            branchDetialFragment.setSharedElementEnterTransition(new DetialTransition());
//            setExitTransition(new Fade());
//            branchDetialFragment.setSharedElementReturnTransition(new DetialTransition());
//            setEnterTransition(new Fade());
            ///////////////////////////////////////////////////////////////////////////
            // 另一种写法 关于shareElement的//
            ///////////////////////////////////////////////////////////////////////////
            setSharedElementReturnTransition(TransitionInflater.from(
                    getActivity()).inflateTransition(R.transition.change_image_trans));
            setExitTransition(TransitionInflater.from(
                    getActivity()).inflateTransition(android.R.transition.fade));

            branchDetialFragment.setSharedElementEnterTransition(TransitionInflater.from(
                    getActivity()).inflateTransition(R.transition.change_image_trans));
            branchDetialFragment.setEnterTransition(TransitionInflater.from(
                    getActivity()).inflateTransition(android.R.transition.fade));

            transitionName = ivImageView.getTransitionName();
            tvTitleTransition = tvTitle.getTransitionName();
        }
        bundle.putSerializable("key", (Serializable) dataBeen);
        bundle.putString("name1",transitionName);
        bundle.putString("name2",tvTitleTransition);

        branchDetialFragment.setArguments(bundle);


        fragmentManager
                .beginTransaction()
                .replace(R.id.container,branchDetialFragment,"BranchDetialFragment")
                .addToBackStack("Payment")
                .addSharedElement(ivImageView,transitionName)
                .addSharedElement(tvTitle,tvTitleTransition)
                .commit();
    }

}
